<template>
	<view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				member_id: '',
				company_id: '',
				userName: '',
				passWord: '',
				wxcode: ''
			}
		},
		onLoad() {
			uni.showLoading({
				title: '努力加载中~~'
			})

			this.token = uni.getStorageSync('btlh_process_token') || '';
			this.member_id = uni.getStorageSync('btlh_process_member_id') || '';
			this.company_id = uni.getStorageSync('bthl_process_LoginCompanyId') || '';
			this.userName = uni.getStorageSync('user_name') || '';
			this.passWord = uni.getStorageSync('user_password') || ''

		},

		onShow() {
			// #ifdef H5
			uni.hideLoading()
			return uni.redirectTo({
				url: 'index'
			})
			// #endif
			this.isHaveToken()
		},

		methods: {
			// 进入应用时 1.先判断token等信息是否可用 2.再判断账号信息是否可用 3.微信小程序登录时判断是否可以用微信登录
			// 以上都不可用时让用户进入登录页面
			isHaveToken() {
				if (this.token && this.member_id && this.company_id) {
					uni.hideLoading()
					console.log('token进入----')
					return uni.switchTab({
						url: '../project/project'
					})
				} else {
					this.isHavePassword()
				}

			},

			async isHavePassword() {
				if (this.userName && this.passWord) {
					const {
						data: res
					} = await this.$myRequest({
						url: 'Public/login_by_password',
						method: 'POST',
						data: {
							username: this.userName,
							password: this.passWord,
						},

					})
					console.log('密码进入----')
					if (res.code != 200) return uni.redirectTo({
						url: 'index'
					})

					// 用户id
					uni.setStorage({
						key: 'btlh_process_member_id',
						data: res.datas.info.member_id
					})

					// 头像
					uni.setStorage({
						key: 'btlh_process_avatarUrl',
						data: res.datas.info.avatar
					})

					// 用户邮箱
					uni.setStorage({
						key: 'btlh_process_mail',
						data: res.datas.info.mail
					})

					// 用户昵称
					uni.setStorage({
						key: 'btlh_process_nickname',
						data: res.datas.info.nickname
					})
					// token
					uni.setStorage({
						key: 'btlh_process_token',
						data: res.datas.info.token
					})
					uni.hideLoading();

					uni.redirectTo({
						url: `/pages/index/firm/firm`
					})


				} else {
					//#ifdef MP-WEIXIN
					this.isHaveWx()
					//#endif

					//#ifdef APP-PLUS
					uni.hideLoading()
					uni.redirectTo({
						url: 'index'
					})
					//#endif
				}


			},

			async isHaveWx() {

				uni.login({
					success: (res) => {
						console.log('res-----' + res.code)
						this.wxcode = res.code
						uni.request({
							url: 'https://control.sxpz.vip/index.php/AppApi/Wx/wxlogin',
							method: 'POST',
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							data: {
								code: this.wxcode
							},
							success: (res) => {
								console.log('微信进入2----' + this.wxcode)
								uni.hideLoading()

								if (res.data.code != 200) return uni.redirectTo({
									url: 'index'
								})
								// 用户id
								uni.setStorage({
									key: 'btlh_process_member_id',
									data: res.data.datas.info.member_id
								})

								// 头像
								uni.setStorage({
									key: 'btlh_process_avatarUrl',
									data: res.data.datas.info.avatar
								})

								// 用户邮箱
								uni.setStorage({
									key: 'btlh_process_mail',
									data: res.data.datas.info.mail
								})

								// 用户昵称
								uni.setStorage({
									key: 'btlh_process_nickname',
									data: res.data.datas.info.nickname
								})
								// token
								uni.setStorage({
									key: 'btlh_process_token',
									data: res.data.datas.info.token
								})
								uni.redirectTo({
									url: `/pages/index/firm/firm`
								})
							},
							fail: () => {
								uni.hideLoading()
								uni.redirectTo({
									url: 'index'
								})
							}
						})


					},
					fail: () => {
						uni.hideLoading()
						uni.redirectTo({
							url: 'index'
						})
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	// 弹窗
	.noticDetails {
		background-color: #fff;
		padding: 20rpx 30rpx;
		width: 630rpx;
		border-radius: 20rpx;
		box-sizing: border-box;
		position: relative;

		.closeIcon {
			position: absolute;
			top: -70rpx;
			right: 0;
		}

		.department {
			width: 400rpx;
			height: 70rpx;
			text-align: center;
			line-height: 70rpx;
			margin-top: 20rpx;
			margin-left: 80rpx;
			border-radius: 30rpx;
			color: #FFFFFF;
		}

	}
</style>
